<?php

namespace App\Admin\Controllers\User;

use App\Models\User;
use App\Models\UserWithdrawal;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class WithdrawalController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '提现申请';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserWithdrawal());

        $grid->column('id', ___('table.zy_user_withdrawal.Id'));
        $grid->column('user_id', ___('table.zy_user_withdrawal.User id'))->display(function ($id){
            $user = User::find($id);
            return $user?$user->name:'---';
        });;
        $grid->column('no', ___('table.zy_user_withdrawal.No'));
        $grid->column('price', ___('table.zy_user_withdrawal.Price'));
        $grid->column('commission', ___('table.zy_user_withdrawal.Commission'));
        $grid->column('status', ___('table.zy_user_withdrawal.Status'))->display(function ($id){
            return [1=>'已申请', 2=>'提现中',3=>'提现成功', 4=>'提现失败'][$id];
        });
        $grid->column('reason', ___('table.zy_user_withdrawal.Reason'));
        $grid->column('create_time', ___('table.zy_user_withdrawal.Create time'));
        $grid->column('update_time', ___('table.zy_user_withdrawal.Update time'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserWithdrawal::findOrFail($id));

        $show->field('id', ___('table.zy_user_withdrawal.Id'));
        $show->field('user_id', ___('table.zy_user_withdrawal.User id'));
        $show->field('no', ___('table.zy_user_withdrawal.No'));
        $show->field('price', ___('table.zy_user_withdrawal.Price'));
        $show->field('commission', ___('table.zy_user_withdrawal.Commission'));
        $show->field('status', ___('table.zy_user_withdrawal.Status'));
        $show->field('reason', ___('table.zy_user_withdrawal.Reason'));
        $show->field('create_time', ___('table.zy_user_withdrawal.Create time'));
        $show->field('update_time', ___('table.zy_user_withdrawal.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserWithdrawal());

        $form->select('user_id', ___('table.zy_user_withdrawal.User id'))->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/ajax/user')->help("输入真实用户姓名");
        $form->text('no', ___('table.zy_user_withdrawal.No'));
        $form->decimal('price', ___('table.zy_user_withdrawal.Price'));
        $form->decimal('commission', ___('table.zy_user_withdrawal.Commission'));
        $form->radio('status', ___('table.zy_user_withdrawal.Status'))->options(
            [1=>'已申请', 2=>'提现中',3=>'提现成功', 4=>'提现失败']
        );
        $form->textarea('reason', ___('table.zy_user_withdrawal.Reason'));
        $form->number('create_time', ___('table.zy_user_withdrawal.Create time'));
        $form->number('update_time', ___('table.zy_user_withdrawal.Update time'));

        return $form;
    }
}
